<?php

namespace plugin\laoha\app\controller;

use support\Request;
use support\Response;
use plugin\laoha\app\model\Nav;
use plugin\laoha\app\common\CateClass;
use plugin\laoha\app\common\Template;
use Intervention\Image\ImageManagerStatic as Image;
use support\exception\BusinessException;
use Exception;

class IndexController
{
    protected $noNeedLogin = '*';

    public function index()
    {
        //$active = rtrim(request()->path(), '/'); 
        //return response($active);
        $init = [
            "title" => "测试",
            "active" => "/app/laoha/post",
            "bread" => "文章列表",
            "user_id" => user_id(),
            "admin_id" => admin_id(),
            "rules" => $this->getRules(),
        ];
        return view('index/index', $init);
    }

    public function test()
    {
        
        $a = plugin_config_merge('morphmaps');
        return json($a);
    }

    public function api()
    {
        return request()->api;
    }

    protected function getRules()
    {
        return [];
    }
    public function navs(Request $request)
    {
        $set = [
            'page_type' => 3,
            'with' => '',
            'order_by' => 'parent_id,sort',
            'no_where' => 'status,active',
        ];
        $model = new Nav;
        $builder = $model->where('status', 1)->where('root', 'default');
        $active = $request->input('active', '/');
        $data = laoha_query($builder, $set, $model);
        $data = $this->change($data, $active);
        return json_ok($data);
    }

    protected function change($data = [], $active = '')
    {
        $dd = [];
        foreach ($data as $k => $v) {
            if (!empty($v['children'])) {
                $v['children'] = $this->change($v['children']);
            }
            $d = [];
            if (empty($v['children'])) {
                if ($v['url'] == '-') {
                    $d['mode'] = 'divider';
                } else {
                    $d['label'] = $v['title'];
                    $d['to'] = $v['url'];
                    if ($v['target']) {
                        $d['target'] = '_blank';
                    }
                    if ($v['icon']) {
                        $d['icon'] = 'fa ' . $v['icon'];
                    }
                    if ($active && $active == $v['url']) {
                        $d['active'] = true;
                    }
                }
            } else {
                $d['label'] = $v['title'];
                $d['unfolded'] = true;
                $d['children'] = $v['children'];
                if ($v['icon']) {
                    $d['icon'] = 'fa ' . $v['icon'];
                }
                if ($active && $active == $v['url']) {
                    $d['active'] = true;
                }
            }
            $dd[] = $d;
        }
        return $dd;
    }



    /**
     * 上传图片
     * @param Request $request
     * @return Response
     * @throws Exception
     
    public function image(Request $request): Response
    {
        $data = $this->base($request, '/upload/img/' . date('Ymd'));
        $realpath = $data['realpath'];
        try {
            $img = Image::make($realpath);
            $max_height = 1170;
            $max_width = 1170;
            $width = $img->width();
            $height = $img->height();
            $ratio = 1;
            if ($height > $max_height || $width > $max_width) {
                $ratio = $width > $height ? $max_width / $width : $max_height / $height;
            }
            $img->resize($width * $ratio, $height * $ratio)->save($realpath);
        } catch (Exception $e) {
            unlink($realpath);
            return json([
                'code'  => 500,
                'msg'  => '处理图片发生错误'
            ]);
        }
        return json_ok([
            'url' => $data['url'],
            'name' => $data['name'],
            'size' => $data['size'],
        ]);
    }
*/

    /**
     * 获取上传数据
     * @param Request $request
     * @param $relative_dir
     * @return array
     * @throws BusinessException

    protected function base(Request $request, $relative_dir): array
    {
        $relative_dir = ltrim($relative_dir, '/');
        $file = current($request->file());
        if (!$file || !$file->isValid()) {
            throw new BusinessException('未找到上传文件', 400);
        }

        $base_dir = base_path() . '/plugin/laoha/public/';
        $full_dir = $base_dir . $relative_dir;
        if (!is_dir($full_dir)) {
            mkdir($full_dir, 0777, true);
        }

        $ext = strtolower($file->getUploadExtension());
        $ext_forbidden_map = ['php', 'php3', 'php5', 'css', 'js', 'html', 'htm', 'asp', 'jsp'];
        if (in_array($ext, $ext_forbidden_map)) {
            throw new BusinessException('不支持该格式的文件上传', 400);
        }

        $relative_path = $relative_dir . '/' . bin2hex(pack('Nn', time(), random_int(1, 65535))) . ".$ext";
        $full_path = $base_dir . $relative_path;
        $file_size = $file->getSize();
        $file_name = $file->getUploadName();
        $mime_type = $file->getUploadMimeType();
        $file->move($full_path);
        $image_with = $image_height = 0;
        if ($img_info = getimagesize($full_path)) {
            [$image_with, $image_height] = $img_info;
            $mime_type = $img_info['mime'];
        }
        return [
            'url'     => "/app/laoha/$relative_path",
            'name'     => $file_name,
            'realpath' => $full_path,
            'size'     => $file_size,
            'mime_type' => $mime_type,
            'image_with' => $image_with,
            'image_height' => $image_height,
            'ext' => $ext,
        ];
    }
     */

}
